package com.kiloai.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by david on 17/7/17.
 */

public class DateUtils {

    private static SimpleDateFormat sf;

    /**
     * 获取系统时间 格式为："yyyy/MM/dd "
     **/
    public static String getCurrentDate() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }

    /**
     * 获取系统时间 格式为："yyyy "
     **/
    public static String getCurrentYear() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy");
        return sf.format(d);
    }

    /**
     * 获取系统时间 格式为："MM"
     **/
    public static String getCurrentMonth() {
        Date d = new Date();
        sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /**
     * 获取系统时间 格式为："dd"
     **/
    public static String getCurrentDay() {
        Date d = new Date();
        sf = new SimpleDateFormat("dd");
        return sf.format(d);
    }

    /**
     * 获取当前月日
     *
     * @return
     */
    public static String getCurrentMonthAndDay() {
        Date d = new Date();
        sf = new SimpleDateFormat("MM月dd日");
        return sf.format(d);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentHour() {
        Date d = new Date();
        sf = new SimpleDateFormat("HH");
        return sf.format(d);
    }

    /**
     * 获取当前时间戳 13位
     *
     * @return
     */
    public static long getCurrentTime() {
        long d = new Date().getTime();
        return d;
    }

    /**
     * 获取当前时间戳 13位
     *
     * @return
     */
    public static String getUnixTime() {
        String d = new Date().getTime() / 1000 + "";
        return d;
    }


    /**
     * 时间戳转换成字符窜
     */
    public static String getDateToString(long time) {
        Date d = new Date(time * 1000);
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }

    /**
     * 从Unix时间戳中获取小时：分
     *
     * @param time
     * @return
     */
    public static String getTimeByTimestamp(long time) {
        Date d = new Date(time * 1000);
        sf = new SimpleDateFormat("HH:mm");
        return sf.format(d);
    }

    /**
     *
     */
    public static String getTimestamp() {
        long d = new Date().getTime();
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /**
     * 从unix时间戳中获取年月日
     */
    public static String getDaybytimeStamp(long t) {
        sf = new SimpleDateFormat("MM-dd");
        return sf.format(t * 1000);
    }

    /**
     * 时间戳中获取年
     */
    public static String getYearFromTime(long time) {
        Date d = new Date(time * 1000);
        sf = new SimpleDateFormat("yyyy");
        return sf.format(d);
    }

    /**
     * 时间戳中获取月
     */
    public static String getMonthFromTime(long time) {
        Date d = new Date(time * 1000);
        sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /**
     * 时间戳中获取日
     */
    public static String getDayFromTime(long time) {
        Date d = new Date(time * 1000);
        sf = new SimpleDateFormat("dd");
        return sf.format(d);
    }

    /**
     * 获取系统时间 格式为："yyyy-MM-dd "
     **/
    public static String getCurrentYearMonthDay() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /**
     * 获取系统时间 格式为："MM-dd "
     **/
    public static String getCurrentMonthDay() {
        Date d = new Date();
        sf = new SimpleDateFormat("MM-dd");
        return sf.format(d);
    }

    /**
     * 将字符串转为时间戳
     */
    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }


    /**
     * 判断是否为今天
     *
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isToday(String time) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.parseLong(time)));
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {

            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);
            if (diffDay == 0) {
                return true;
            }
        }
        return false;

    }

    /**
     * 判断是否为昨天
     *
     * @return
     * @throws ParseException
     */
    public static boolean isYesterday(String time) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.parseLong(time)));
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);
            if (diffDay == -1) {
                return true;
            }
        }
        return false;

    }

}